package cn.nai.DemoRan.demo02;

import javax.swing.*;
import java.awt.*;

public class AlgoFrame extends JFrame {
    private int canvasWidth;
    private int canvasHeight;
    public AlgoFrame(String title,int canvasWidth,int canvasHeight){
        super(title);
        this.canvasWidth = canvasWidth;
        this.canvasHeight = canvasHeight;

        AlgoCanvas canvas = new AlgoCanvas();
        canvas.setPreferredSize(new Dimension(canvasWidth,canvasHeight));
        this.setContentPane(canvas);//附上画布大小
        pack();
        this.setSize(canvasWidth,canvasHeight);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    public AlgoFrame(String title){
        this(title,1024,768);//默认
    }
    private Circle2[] circle2s;
    public void render(Circle2[] circle2s){
        this.circle2s = circle2s;
        this.repaint();//重新刷新
    }
    public int getCanvasWidth(){return canvasWidth;}
    public int getCanvasHeight(){return canvasHeight;}
    private Object data;
    //*****设计自己的数据
    public void render(Object data){
        this.data = data;
        repaint();
    }
    private class AlgoCanvas extends JPanel{
        public AlgoCanvas(){
            super(true);//支持双缓存
        }
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
//            g.drawOval(50,50,300,300);
            Graphics2D g2d = (Graphics2D)g;
            /**
             * 抗锯齿操作
             * */
            RenderingHints hints = new RenderingHints(
                    RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON   );
            g2d.addRenderingHints(hints);
            /**
            * 具体描绘
            * */
        //****自己的data   下面的是一份样例



            /**
             *
            //设置笔画宽度
           AlgoVisHelper.setStrokeWidth(g2d,5);//引入
//            g2d.setColor(Color.RED);//水彩笔芯
            AlgoVisHelper.setColor(g2d,Color.RED);
//            Ellipse2D circle = new Ellipse2D.Double(50,50,300,300);
//            g2d.draw(circle);
            AlgoVisHelper.strokeCircle(g2d,canvasWidth/2,canvasHeight/2,200);
//            g2d.setColor(Color.BLUE);//水彩笔芯
            AlgoVisHelper.setColor(g2d,Color.BLUE);
            AlgoVisHelper.fillCircle(g2d,canvasWidth/2,canvasHeight/2,200);
//画⚪
             */

            AlgoVisHelper.setStrokeWidth(g2d,1);
            AlgoVisHelper.setColor(g2d,Color.RED);
            for (Circle2 circle2 : circle2s) {
                if (!circle2.isFilled)
                    AlgoVisHelper.strokeCircle(g2d, circle2.x, circle2.y, circle2.getR());
                else
                    AlgoVisHelper.fillCircle(g2d, circle2.x, circle2.y, circle2.getR());
            }
        }
        @Override
        public Dimension getPreferredSize(){
            return new Dimension(canvasWidth,canvasHeight);
        }
    }


}
